<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 300 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="openDrawer('add')"
            round>
            添加面经
          </el-button>

        </div>
      </template>
      <!-- 表格 -->
     <el-table :data="list" stripe style="width: 100%">
       <el-table-column prop="stem" label="标题" width="350"></el-table-column>
       <el-table-column prop="creator" label="作者"></el-table-column>
       <el-table-column prop="likeCount" label="点赞"></el-table-column>
       <el-table-column prop="views" label="浏览数"></el-table-column>
       <el-table-column prop="createdAt" label="更新时间" width="180"></el-table-column>
       <el-table-column  label="操作" width="200">
         <template #default="{ row }">
           <div class="actions">
             <i class="el-icon-view"  @click="openDrawer('preview',row.id)"></i>
             <i class="el-icon-edit-outline"  @click="openDrawer('edit',row.id)"></i>
             <i class="el-icon-delete" @click="del(row.id)"></i>
            </div>
          </template>
       </el-table-column>
     </el-table>
     <!-- 分页 -->
      <el-pagination
      background=""
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-size="pageSize"
      layout="total, prev, pager, next"
      :total="total">
    </el-pagination>
    <!-- 抽屉 -->
          <el-drawer
           :title="drawerTitle"
           :visible.sync="isShowDrawre"
           :before-close="handleClose">
<div v-if="drawerType === 'preview'" class="article-preview">
  <h5>{{ form.stem }}</h5>
  <div v-html="form.content"></div>
</div>
           <el-form ref="form" label-width="80px" :model="form" :rules="rules" v-else>
              <el-form-item label="标题" prop="stem">
                <el-input v-model="form.stem" placeholder="输入面经标题"></el-input>
              </el-form-item>
                  <el-form-item label="内容" prop="content">
                        <quill-editor v-model="form.content" @blur="$refs.form.validateField('content')"></quill-editor>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary"  @click="onAdd">确认</el-button>
                    <el-button>取消</el-button>
                  </el-form-item>
           </el-form>
          </el-drawer>
    </el-card>
  </div>
</template>

<script>
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import { article, createArticle, removeArticle, getArticleDetail, updateArticle } from '@/api/article'
export default {
  name: 'article-page',
  data () {
    return {
      total: 0, // 总页数
      list: [], // 放数据
      current: 1, // 第几页
      pageSize: 10, // 一页十条
      isShowDrawre: false,
      drawerType: '', // 添加、修改、预览等类型
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [{ required: true, message: '请输入面经标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入面经标题', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async fn () {
      const res = await article({
        current: this.current,
        pageSize: this.pageSize
      })
      console.log(res)
      this.list = res.data.rows
      this.total = res.data.total// 储存的总页数
    },
    handleCurrentChange (val) {
      // 处理当前页变化
      // console.log(val)
      this.current = val
      this.fn()
    },
    handleClose () {
      this.isShowDrawre = false
      this.$refs.form.resetFields()
    },
    async  openDrawer (type, id) {
      this.isShowDrawre = true
      this.drawerType = type
      // 打开抽屉

      if (type !== 'add') {
        const res = await getArticleDetail(id)
        console.log(res)
        this.form = {
          ...res.data
        }
      }
    },
    // 添加
    onAdd () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.drawerType === 'add') {
            const { stem, content } = this.form
            await createArticle({ stem, content })
            this.$message.success('添加成功')
            this.current = 1
          }
          if (this.drawerType === 'edit') {
            const { stem, content, id } = this.form
            await updateArticle({ stem, content, id })
            this.$message.success('修改成功')
          }
          this.fn()
          this.handleClose()
        }
      })
    },
    // 删除
    del (id) {
      this.$confirm('你确定要删除吗？', '提示')
        .then(async () => {
          // 说明点击了确认
          // console.log(id)
          await removeArticle(id)
          this.$message.success('删除成功')
          this.fn()
        })
        .catch(() => {
          // 说明点击了取消
        })
    }
  },
  created () {
    this.fn()
  },
  computed: {
    drawerTitle () {
      let title = '大标题'
      if (this.drawerType === 'add') title = '添加面经'
      if (this.drawerType === 'preview') title = '面经预览'
      if (this.drawerType === 'edit') title = '修改面经'
      return title
    }

  },
  components: {
    quillEditor
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
